package edu.ccut.saturn.studio.dialog.webcontextdialog;

import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnSWTUtils;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnWebContextNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnWebContextParameter;
import edu.ccut.saturn.studio.util.ConversionUtil;

public class WebContextDialogForAdd extends TitleAreaDialog {

	private Shell shell;

	private Text IDText, keyText, newKeyText;

	private Label IDLabel, scopeLabel, keyLabel, imageLabel, newScopeLabel,
			newKeyLabel;

	private Combo scopeCombo, newScopeCombo;

	private Button deleteYes, deleteNo;

	private SaturnWebContextNode saturnWebContextNode;

	private List<SaturnWebContextParameter> saturnWebContextParameterList;

	private SaturnWebContextParameter saturnWebContextParameter;

	public WebContextDialogForAdd(Shell parentShell,
			List<SaturnWebContextParameter> saturnWebContextParameterList,
			SaturnWebContextNode saturnWebContextNode) {
		super(parentShell);
		this.shell = parentShell;
		this.saturnWebContextParameterList = saturnWebContextParameterList;
		this.saturnWebContextNode = saturnWebContextNode;
		this.saturnWebContextParameter = new SaturnWebContextParameter();
	}

	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(Messages.getString("WebContextDialogForAdd.AddContextPara"));  //$NON-NLS-1$
		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		getShell().setText(Messages.getString("WebContextDialogForAdd.ParaAttributeCofig")); //$NON-NLS-1$
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		Composite area = (Composite) super.createDialogArea(parent);

		final Composite composite = new Composite(area, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		composite.setLayout(gridLayout);

		IDLabel = new Label(composite, SWT.NONE);
		IDLabel.setText(Messages.getString("WebContextDialogForAdd.ContextID")); //$NON-NLS-1$

		IDText = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_iDText = new GridData(SWT.FILL, SWT.FILL, true,
				false, 2, 1);
		IDText.setLayoutData(gd_iDText);
		IDText.setText(String.valueOf(saturnWebContextParameterList.size() + 1));

		scopeLabel = new Label(composite, SWT.NONE);
		scopeLabel.setLayoutData(new GridData());
		scopeLabel.setText(Messages.getString("WebContextDialogForAdd.ContextScope")); //$NON-NLS-1$

		scopeCombo = new Combo(composite, SWT.READ_ONLY);
		final GridData gd_scopeCombo = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 2, 1);
		scopeCombo.setLayoutData(gd_scopeCombo);
		scopeCombo.setItems(SelectionCandidate.scopes);
		scopeCombo.select(1);
		scopeCombo.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent modifyevent) {
				keyText.setText("");
			}
		});

		keyLabel = new Label(composite, SWT.NONE);
		keyLabel.setLayoutData(new GridData());
		keyLabel.setText(Messages.getString("WebContextDialogForAdd.ContextKeyValue")); //$NON-NLS-1$

		keyText = new Text(composite, SWT.BORDER);
		keyText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate ();
			}
		});
		keyText.setTextLimit(100);
		final GridData gd_keyText = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		keyText.setLayoutData(gd_keyText);

		imageLabel = new Label(composite, SWT.NONE);
		imageLabel.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
		imageLabel.setLayoutData(new GridData());
		imageLabel.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				imageLabel
						.setImage(Activator.getImage(Images.AMPLIFY_MINUS));
			}

			public void mouseExit(final MouseEvent e) {
				imageLabel.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
			}
		});
		imageLabel.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
				ElementListSelectionDialog dialog1 = new ElementListSelectionDialog(
						shell, new SaturnSWTUtils.SaturnLabelDefaultProvider());
				dialog1.setTitle(Messages.getString("WebContextDialogForAdd.ChooseKeyValue"));  //$NON-NLS-1$
				dialog1.setElements(saturnWebContextNode
						.getValueKeyByScope(ConversionUtil
								.switchScopeToDigital(scopeCombo.getText()
										.trim())));
				if (dialog1.open() == Window.OK) {
					keyText.setText((String) dialog1.getFirstResult());
				}
			}
		});

		final Group group = new Group(composite, SWT.NONE);
		group.setLayout(new FormLayout());
		group.setText(Messages.getString("WebContextDialogForAdd.IfDelete"));  //$NON-NLS-1$
		final GridData gd_group = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 3, 1);
		gd_group.heightHint = 25;
		gd_group.widthHint = 150;
		group.setLayoutData(gd_group);

		deleteYes = new Button(group, SWT.RADIO);
		deleteYes.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (deleteYes.getSelection() == true) {
					newScopeCombo.setEnabled(false);
					newKeyText.setText("");
					newKeyText.setEnabled(false);
					validate ();
				}
			}
		});
		final FormData fd_deleteYes = new FormData();
		fd_deleteYes.bottom = new FormAttachment(0, 21);
		fd_deleteYes.top = new FormAttachment(0, 5);
		fd_deleteYes.right = new FormAttachment(0, 38);
		fd_deleteYes.left = new FormAttachment(0, 10);
		deleteYes.setLayoutData(fd_deleteYes);
		deleteYes.setText(Messages.getString("WebContextDialogForAdd.yes")); //$NON-NLS-1$

		deleteNo = new Button(group, SWT.RADIO);
		deleteNo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				newScopeCombo.setEnabled(true);
				newKeyText.setEnabled(true);
				validate ();
			}
		});
		deleteNo.setSelection(true);
		final FormData fd_deleteNo = new FormData();
		fd_deleteNo.left = new FormAttachment(deleteYes, 35, SWT.RIGHT);
		fd_deleteNo.bottom = new FormAttachment(0, 21);
		fd_deleteNo.top = new FormAttachment(0, 5);
		deleteNo.setLayoutData(fd_deleteNo);
		deleteNo.setText(Messages.getString("WebContextDialogForAdd.no")); //$NON-NLS-1$

		newScopeLabel = new Label(composite, SWT.NONE);
		newScopeLabel.setText(Messages.getString("WebContextDialogForAdd.NewScope")); //$NON-NLS-1$

		newScopeCombo = new Combo(composite, SWT.READ_ONLY);
		final GridData gd_newScopeText = new GridData(SWT.FILL, SWT.CENTER,
				true, false, 2, 1);
		newScopeCombo.setLayoutData(gd_newScopeText);
		newScopeCombo.setItems(SelectionCandidate.scopes);
		newScopeCombo.select(2);

		newKeyLabel = new Label(composite, SWT.NONE);
		newKeyLabel.setText(Messages.getString("WebContextDialogForAdd.NewKeyValue")); //$NON-NLS-1$

		newKeyText = new Text(composite, SWT.BORDER);
		newKeyText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				validate ();
			}
		});
		newKeyText.setTextLimit(100);
		final GridData gd_newKeyText = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 2, 1);
		newKeyText.setLayoutData(gd_newKeyText);
		new Label(composite, SWT.NONE);

		if (deleteYes.getSelection() == true) {
			newScopeCombo.setEnabled(false);
			newKeyText.setEnabled(false);
		}
		if (deleteNo.getSelection() == true) {
			newScopeCombo.setEnabled(true);
			newKeyText.setEnabled(true);
		}
		validate();
		return area;
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			saturnWebContextParameter.setId(IDText.getText().trim());
			saturnWebContextParameter.setKey(keyText.getText().trim());
			saturnWebContextParameter.setScope(scopeCombo.getText().trim());
			if (deleteYes.getSelection() == true) {
				saturnWebContextParameter.setIsDelete("true");
			}
			if (deleteNo.getSelection() == true) {
				saturnWebContextParameter.setIsDelete("false");
			}
			saturnWebContextParameter.setNewKey(newKeyText.getText().trim());
			saturnWebContextParameter
					.setNewScope(newScopeCombo.getText().trim());

			saturnWebContextParameterList.add(saturnWebContextParameter);
		}
		super.buttonPressed(buttonId);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(460, 320);
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}
	
	private boolean validate (){
		
		
		if (SaturnValidation.isEmpty(keyText.getText().trim())) {
			setErrorMessage(Messages.getString("WebContextDialogForAdd.keyIsNotNull")); //$NON-NLS-1$
	
			return false;
		} else if (deleteNo.getSelection() == true || deleteNo.getSelection()!= false) {
			if (SaturnValidation.isEmpty(newKeyText.getText())) {
				setErrorMessage(Messages.getString("WebContextDialogForAdd.NewKeyIsNotNull")); //$NON-NLS-1$
				getButton(IDialogConstants.OK_ID).setEnabled(false);
				return false;
			}else {
				getButton(IDialogConstants.OK_ID).setEnabled(true);
				setErrorMessage(null);
				return true;	
			}
		}
		getButton(IDialogConstants.OK_ID).setEnabled(true);
		setErrorMessage(null);
		return true;
	}
	
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(false);
	}
}